#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# shellcheck source=start-utils
. "${SCRIPTS:-/}start-utils"
isDebugging && set -x

: "${CANYON_BUILD:=lastSuccessfulBuild}"

if [ "${VERSION}" != "b1.7.3" ]; then
  log "ERROR: Canyon server type only supports VERSION=b1.7.3"
  exit 1
fi

canyonJob="https://ci.velocitypowered.com/job/Canyon"

buildRelPath=$(
  curl -fsSL "${canyonJob}/${CANYON_BUILD}/api/json" |
    jq -r '.artifacts[0].relativePath'
)
buildNumber=$(
  curl -fsSL "${canyonJob}/${CANYON_BUILD}/api/json" |
    jq -r '.number'
)
baseName=$(basename "${buildRelPath}")

# Add build tag to non-tagged builds for version detection
if [ ${baseName%.*} = 'canyon-server' ]; then
    export SERVER=${baseName%.*}_build${buildNumber}.jar
else
    export SERVER="${baseName}"
fi

if [ ! -f "$SERVER" ]; then
    downloadUrl="${canyonJob}/${CANYON_BUILD}/artifact/${buildRelPath}"
    log "Downloading Canyon build ${buildNumber} from $downloadUrl ..."
    curl -fsSL -o "$SERVER" "$downloadUrl"
    if [ ! -f "$SERVER" ]; then
      log "ERROR: failed to download from $downloadUrl (status=$?)"
      exit 3
    fi
fi

# Normalize on Spigot for later operations
export FAMILY=SPIGOT

exec ${SCRIPTS:-/}start-spiget "$@"
